package com.itheima.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.sms.SmsFlashPromotionProductRelationMapper;
import com.itheima.pojo.pms.PmsProduct;
import com.itheima.pojo.sms.SmsFlashPromotion;
import com.itheima.pojo.sms.SmsFlashPromotionProductRelation;
import com.itheima.pojo.sms.SmsFlashPromotionSession;
import com.itheima.service.sms.SmsFlashPromotionProductRelationService;
import com.itheima.service.sms.SmsFlashPromotionService;
import com.itheima.service.sms.SmsFlashPromotionSessionService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class SmsFlashPromotionProductRelationServiceImpl extends ServiceImpl<SmsFlashPromotionProductRelationMapper, SmsFlashPromotionProductRelation> implements SmsFlashPromotionProductRelationService {
    @DubboReference
    private SmsFlashPromotionService smsFlashPromotionService;
    @DubboReference
    private SmsFlashPromotionSessionService smsFlashPromotionSessionService;
    /*修改关联信息*/
    @Override
    public void updateBy(SmsFlashPromotionProductRelation sms) {

        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsFlashPromotionProductRelation::getId,sms.getId());
        List<SmsFlashPromotionProductRelation> list = this.list(queryWrapper);
        List<SmsFlashPromotionProductRelation> collect = list.stream().map(item -> {
            item.setFlashPromotionId(sms.getFlashPromotionId());
            item.setFlashPromotionCount(sms.getFlashPromotionCount());
            item.setFlashPromotionPrice(new BigDecimal(sms.getFlashPromotionPrice().doubleValue()));
            item.setFlashPromotionLimit(sms.getFlashPromotionLimit());
            item.setProductId(sms.getProductId());
            item.setFlashPromotionSessionId(sms.getFlashPromotionSessionId());
            item.setSort(sms.getSort());
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(collect);
    }

    /*分页*/
    @Override
    public PageInfo listAll(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        SmsFlashPromotion smsFlashPromotion = smsFlashPromotionService.getById(flashPromotionId);
        String title = smsFlashPromotion.getTitle();
        SmsFlashPromotionSession smsFlashPromotionSession = smsFlashPromotionSessionService.getById(flashPromotionSessionId);
        String name = smsFlashPromotionSession.getName();
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();



        return null;
    }

    /*批量选择商品添加关联*/
    @Override
    public void create(List<SmsFlashPromotionProductRelation> smss) {
        for (SmsFlashPromotionProductRelation sfpr : smss) {
            LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SmsFlashPromotionProductRelation::getId,sfpr.getId());
            List<SmsFlashPromotionProductRelation> list = this.list(queryWrapper);
            List<SmsFlashPromotionProductRelation> collect = list.stream().map(item -> {
                item.setSort(sfpr.getSort());
                item.setFlashPromotionId(sfpr.getFlashPromotionSessionId());
                item.setProductId(sfpr.getProductId());
                item.setFlashPromotionPrice(sfpr.getFlashPromotionPrice());
                item.setFlashPromotionLimit(sfpr.getFlashPromotionLimit());
                item.setFlashPromotionSessionId(sfpr.getFlashPromotionSessionId());
                return item;
            }).collect(Collectors.toList());
            this.updateBatchById(list);

        }




    }
}
